package com.qf.dao.impl;

import com.qf.dao.UserDao;
import com.qf.pojo.User;
import com.qf.utils.JdbcUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.SQLException;

/**
 * @author Kuke
 * @date 2021/9/2
 * 针对用户的数据接口实现
 */
public class UserDaoImpl implements UserDao {
    /**
     * 根据用户名查询用户实体
     * @param username  前台输入用户名
     * @return 返回用户实体
     */
    @Override
    public User selectUserByName(String username) {
        //执行对象
        QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource()) ;
        //sql
        String sql  = "select * from user where username = ?" ;
        //执行查询
        try {
            User user = qr.query(sql, new BeanHandler<User>(User.class), username);
            return user ;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 添加用户数据
     * @param user  用户实体
     * @return  返回影响的行数
     */
    @Override
    public int addUser(User user) {
        //执行对象
        QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource()) ;
        //sql
        String sql = "insert into user(uid,username,password,name,email,birthday,sex,state,code) values(?,?,?,?,?,?,?,?,?)" ;
        //更新
        try {

            int count = qr.update(sql,
                    user.getUid(),
                    user.getUsername(),
                    user.getPassword(),
                    user.getName(),
                    user.getEmail(),
                    user.getBirthday(),
                    user.getSex(),
                    user.getState(),
                    user.getCode()
            );
            return count ;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 根据用户名激活码查询用户
     * @param code  用户的激活码
     * @return  返回用户实体
     */
    @Override
    public User selectUserByCode(String code) {
        //执行对象
        QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource()) ;
        //sql
        String sql = "select * from user where code = ?" ;
        //查询
        try {
            User user = qr.query(sql, new BeanHandler<User>(User.class), code);
            return  user ;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 更改用户的激活状态
     * @param user  需要被更新用户的实体
     */
    @Override
    public void updateUser(User user) {
        //执行对象
        QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource()) ;
        //sql
        String sql = "update user set state = ? where uid = ? " ;
        //执行
        try {
            qr.update(sql,
                    user.getState(),
                    user.getUid()) ;
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    /**
     * 通过用户名和密码寻找用户
     * @param username 用户名
     * @param password  密码
     * @return  返回用户实体
     */
    @Override
    public User selectUserByNameAndPwd(String username, String password) throws SQLException {
        //执行对象
        QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource()) ;
        //sql
        String sql = "select * from user where username  = ? and password = ?" ;
        //执行查询
        User user = qr.query(sql, new BeanHandler<User>(User.class), username, password);
        return user;
    }
}
